/*
    Copyright(C) 2010 Ying-Chun Liu(PaulLiu). All rights reserved.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

package org.debian.paulliu.plft;

import java.io.*;
import java.lang.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.awt.datatransfer.*;
import java.awt.image.*;
import java.applet.*;
import java.util.logging.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;

public class StunPerformer
{

    private String stunServer;
    private int stunServerPort;
    private java.net.InetAddress localAddress;
    private de.javawi.jstun.test.DiscoveryInfo discoveryInfo;

    public StunPerformer(String stunServer, int stunServerPort) {
	this.stunServer = stunServer;
	this.stunServerPort = stunServerPort;
	perform();
    }

    private void perform() {
	DatagramSocket socket=null;
	InetAddress serverAddr=null;
	try {
	    serverAddr = InetAddress.getByName(this.stunServer);
	} catch (java.net.UnknownHostException e) {
	    PLFT.log(Level.SEVERE,"Unknown host "+this.stunServer);
	}
	try {
	    socket = new DatagramSocket();
	} catch (java.net.SocketException e) {
	    PLFT.log(Level.SEVERE,"STUN: Error on DatagramSocket creation");
	}
	socket.connect(serverAddr, this.stunServerPort);
	this.localAddress = socket.getLocalAddress();

	de.javawi.jstun.test.DiscoveryTest dt1 = new de.javawi.jstun.test.DiscoveryTest(this.localAddress, this.stunServer, this.stunServerPort);
	try {
	    this.discoveryInfo = dt1.test();
	} catch (Exception e) {
	    PLFT.log(Level.SEVERE,"STUN: Can't use jstun to do stun test");
	    this.discoveryInfo = null;
	}
    }

    public de.javawi.jstun.test.DiscoveryInfo getDiscoveryInfo() {
	return this.discoveryInfo;
    }

    public String getNATType() {
	de.javawi.jstun.test.DiscoveryInfo discoveryInfo;
	String ret=null;
	discoveryInfo = getDiscoveryInfo();
	if (discoveryInfo.isError()) {
	    ret="error";
	} else if (discoveryInfo.isBlockedUDP()) {
	    ret="UDP blocked";
	} else if (discoveryInfo.isSymmetricUDPFirewall()) {
	    ret="Symmetric Firewall";
	} else if (discoveryInfo.isOpenAccess()) {
	    ret="Open Internet";
	} else if (discoveryInfo.isFullCone()) {
	    ret="Full-cone NAT";
	} else if (discoveryInfo.isSymmetric()) {
	    ret="Symmetric NAT";
	} else if (discoveryInfo.isRestrictedCone()) {
	    ret="Restricted cone NAT";
	} else if (discoveryInfo.isPortRestrictedCone()) {
	    ret="Restricted port NAT";
	}
	return ret;
    }

    public static void main(String[] args) {
	StunPerformer stunPerformer = new StunPerformer("stun.sipgate.net", 3478);
	stunPerformer.perform();
	System.out.println(stunPerformer.getNATType());
    }

}
